/**
 * <p>Title: PostAction.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2010/01/12 23:32:11
 * @version 1.0
 */
package net.donotes.blog.action;

import java.util.Date;

import net.donotes.blog.dao.CategoryDao;
import net.donotes.blog.dao.CommentDao;
import net.donotes.blog.dao.OptionsDao;
import net.donotes.blog.dao.PostDao;
import net.donotes.blog.pojo.Category;
import net.donotes.blog.pojo.Comment;
import net.donotes.blog.pojo.Options;
import net.donotes.blog.pojo.Post;
import net.donotes.blog.service.PostService;
import net.donotes.util.Constants;
import net.donotes.util.MD5Util;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author WangJun
 *
 */
public class PostAction extends BaseAction implements ModelDriven<Post> {

	private static final long serialVersionUID = 1L;
	private String txtCategory = "";
	private Comment comment = new Comment();
	private Long postId = null;
	
	@Override
    public Post getModel() {
        return post;
    }

    private Post post = new Post();
    
	public String execute() {
		PostService postServiec = new PostService();
        // HttpServletRequest request = ServletActionContext.getRequest();
		
		if (this.postId != null && !this.postId.equals("")) {
			Post result = postServiec.getPost(this.postId);
			
			if (result != null) {
				// TODO DEL OK ?
				post.setAuthor(result.getAuthor());
				post.setContent(result.getContent());
				post.setCategory(result.getCategory());
				post.setCommentCounter(result.getCommentCounter());
				post.setCreateDate(result.getCreateDate());
				post.setVisitCounter(result.getVisitCounter());
				post.setId(result.getId());
				post.setStatus(result.getStatus());
				post.setTags(result.getTags());
				post.setTitle(result.getTitle());
				post.setType(result.getType());
				post.setUpdateDate(result.getUpdateDate());
			}
		}

		return SUCCESS;
	}    
    
    public String updPost() {
    	PostService postServiec = new PostService();
    	OptionsDao optionsDao = new OptionsDao();
    	
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
    	
        post.setAuthor(user);
        //post.setStatus(0);
        //post.setType(0);
    	//post.setCreateDate();
    	//post.setUpdateDate(new Date());
    	
    	if (post.getId() !=  null && !post.getId().equals("")) {
    		postServiec.updPost(post);
    	} else {
            post.setVisitCounter(0);
        	postServiec.addPost(post);
        	optionsDao.updOptions(
        			new Options(
        					Constants.OPTION_POST_COUNTER, 
        					String.valueOf(Integer.parseInt(optionsDao.getOptions(Constants.OPTION_POST_COUNTER)) + 1)
        				)
        			);
    	}
    	
    	return SUCCESS;
    }
    
    public String addCategory() {
    	CategoryDao categoryDao = new CategoryDao();  
    	// TODO slug
    	categoryDao.addCategory(new Category(this.txtCategory, this.txtCategory, 0));
    	return SUCCESS;
    }
    
    public String delCategory() {
    	CategoryDao categoryDao = new CategoryDao();    	
    	categoryDao.delCategory(this.txtCategory);
    	
    	String str = "" + "a";
    	str = str + "";
    	return SUCCESS;
    }
    
    public String addComment() {
    	CommentDao commentsDao = new CommentDao();
    	if (comment.getAuthorMail() != null && comment.getAuthorMail() != "") {
        	comment.setAuthorGravatar(MD5Util.md5Hex(comment.getAuthorMail()));
    	} else {
        	comment.setAuthorGravatar("ad516503a11cd5ca435acc9bb6523536");
    	}
    	comment.setCreateDate(new Date());
    	commentsDao.addComment(comment);
    	
    	PostDao postDao = new PostDao();
    	
    	Post post = postDao.getPost(comment.getPostId());
    	post.setCommentCounter(post.getCommentCounter() + 1);
    	postDao.updPost(post);

    	return SUCCESS;
    }
    
	/**
	 * @return the txtCategory
	 */
	public String getTxtCategory() {
		return txtCategory;
	}

	/**
	 * @param txtCategory the txtCategory to set
	 */
	public void setTxtCategory(String txtCategory) {
		this.txtCategory = txtCategory;
	}

	/**
	 * @return the comment
	 */
	public Comment getComment() {
		return comment;
	}

	/**
	 * @param comment the comment to set
	 */
	public void setComments(Comment comment) {
		this.comment = comment;
	}
	
	/**
	 * @return the postId
	 */
	public Long getPostId() {
		return postId;
	}

	/**
	 * @param postId the postId to set
	 */
	public void setPostId(Long postId) {
		this.postId = postId;
	}
}